package com.liduanwei.springboot.controller

import com.baomidou.mybatisplus.mapper.EntityWrapper
import com.baomidou.mybatisplus.plugins.Page
import com.liduanwei.springboot.entity.UsNewestVersionConfig
import com.liduanwei.springboot.service.IUsNewestVersionConfigService
import com.jinrustar.op.core.base.BaseController
import com.jinrustar.op.core.support.Assert
import com.jinrustar.op.core.util.InstanceUtil
import com.jinrustar.op.core.util.RedisUtil
import com.jinrustar.op.core.util.Request2ModelUtil
import com.jinrustar.op.core.util.WebUtil
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.ui.ModelMap
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletRequest

@RestController
@Api(description = "机器端最新版本配置", value = "机器端最新版本配置")
@RequestMapping(value = "version/newest")
class UsVersionNewestConfigController : BaseController() {

    @Autowired
    private val usNewestVersionConfigService: IUsNewestVersionConfigService? = null


    @ApiOperation("查看")
    @RequestMapping(value = "/list", method = arrayOf(RequestMethod.GET, RequestMethod.POST))
    fun list(request: HttpServletRequest, modelMap: ModelMap,
             @ApiParam(value = "页码") @RequestParam(value = "pageNum", required = false, defaultValue = "1") pageNum: Int,
             @ApiParam(value = "size") @RequestParam(value = "pageSize", required = false, defaultValue = "50") pageSize: Int): Any {
        var params = WebUtil.getParameterMap(request)

        return setSuccessModelMap(modelMap, usNewestVersionConfigService!!.queryAll(params))
    }


    /*查询有无新版*/
    @ApiOperation("查询有无新版")
    @RequestMapping(value = "/{machineId}/version/check", method = arrayOf(RequestMethod.GET, RequestMethod.POST))
    fun check(request: HttpServletRequest, modelMap: ModelMap,
              @PathVariable(value = "machineId") machineId: String,
              @ApiParam(value = "httpServiceVersionCode") @RequestParam(value = "httpServiceVersionCode", required = false) httpServiceVersionCode: Integer?,
              @ApiParam(value = "machineVersionCode") @RequestParam(value = "machineVersionCode", required = false) machineVersionCode: Integer?,
              @ApiParam(value = "uiVersionCode") @RequestParam(value = "uiVersionCode", required = false) uiVersionCode: Integer?,
              @ApiParam(value = "otherFilesVersionCode") @RequestParam(value = "otherFilesVersionCode", required = false) otherFilesVersionCode: Integer?,
              @ApiParam(value = "selfVersionCode") @RequestParam(value = "selfVersionCode", required = false) selfVersionCode: Integer?
    ): Any {
        /*默认取缓存*/
        /*todo*/

        var newestVersionConfig: UsNewestVersionConfig? = usNewestVersionConfigService!!.findByMachineId(machineId)

        var simpleVersionInfos: MutableList<SimpleVersionInfo> = arrayListOf()
        if (newestVersionConfig != null) {

            val hasNewHttpServiceVersion: Boolean = newestVersionConfig.httpServiceVersionCode != httpServiceVersionCode?.toInt() ?: 1
            val hasNewMachineVersion = newestVersionConfig.machineVersionCode != machineVersionCode?.toInt() ?: 1
            val hasNewUiVersion = newestVersionConfig.uiVersionCode != uiVersionCode?.toInt() ?: 1
            val hasNewOtherFilesVersion = newestVersionConfig.otherFilesVersionCode != otherFilesVersionCode?.toInt() ?: 1
            val hasNewSelfVersion = newestVersionConfig.selfVersionCode != selfVersionCode?.toInt() ?: 1


            if (hasNewHttpServiceVersion) {
                simpleVersionInfos.add(SimpleVersionInfo("httpServiceVersion", newestVersionConfig.httpServiceVersionCode, newestVersionConfig.httpServiceVersionName, newestVersionConfig.httpServiceVersionUrl))
            }

            if (hasNewMachineVersion) {
                simpleVersionInfos.add(SimpleVersionInfo("machineVersion", newestVersionConfig.machineVersionCode, newestVersionConfig.machineVersionName, newestVersionConfig.machineVersionUrl))
            }
            if (hasNewUiVersion) {
                simpleVersionInfos.add(SimpleVersionInfo("uiVersion", newestVersionConfig.uiVersionCode, newestVersionConfig.uiVersionName, newestVersionConfig.uiVersionUrl))
            }

            if (hasNewOtherFilesVersion) {
                simpleVersionInfos.add(SimpleVersionInfo("otherFilesVersion", newestVersionConfig.otherFilesVersionCode, newestVersionConfig.otherFilesVersionName, newestVersionConfig.otherFilesVersionUrl))
            }

            if (hasNewSelfVersion) {
                simpleVersionInfos.add(SimpleVersionInfo("selfVersion", newestVersionConfig.selfVersionCode, newestVersionConfig.selfVersionName, newestVersionConfig.selfVersionUrl))
            }

            return setSuccessModelMap(modelMap, simpleVersionInfos)
        }

        return setSuccessModelMap(modelMap)
    }

    data class SimpleVersionInfo(var type: String, var versionCode: Int, var versionName: String, var versionUrl: String)

    @ApiOperation("查看指定机器最新各版本配置(含新版本下载链接)")
    @RequestMapping(value = "/{machineId}/view", method = arrayOf(RequestMethod.GET, RequestMethod.POST))
    fun view(request: HttpServletRequest, modelMap: ModelMap,
             @PathVariable(value = "machineId") machineId: String
    ): Any {
        /*默认取缓存*/

        val newestVersionConfigPage: Page<UsNewestVersionConfig> = usNewestVersionConfigService!!.queryAll(InstanceUtil.newHashMap("machineId", machineId))
        var newestVersionConfig: UsNewestVersionConfig? = null
        if (newestVersionConfigPage.total != 0) {
            newestVersionConfig = newestVersionConfigPage.records[0]
        }
        return setSuccessModelMap(modelMap, newestVersionConfig)
    }


    @ApiOperation(value = "更新/新增")
    @RequestMapping(value = "/{machineId}/update", method = arrayOf(RequestMethod.POST))
    fun add(request: HttpServletRequest, modelMap: ModelMap,
            @PathVariable(value = "machineId") machineId: String,
            @ApiParam(value = "httpServiceVersionName") @RequestParam(value = "httpServiceVersionName", required = false) httpServiceVersionName: String?,
            @ApiParam(value = "httpServiceVersionUrl") @RequestParam(value = "httpServiceVersionUrl", required = false) httpServiceVersionUrl: String?,
            @ApiParam(value = "httpServiceVersionCode") @RequestParam(value = "httpServiceVersionCode", required = false) httpServiceVersionCode: Integer?,

            @ApiParam(value = "machineVersionName") @RequestParam(value = "machineVersionName", required = false) machineVersionName: String?,
            @ApiParam(value = "machineVersionUrl") @RequestParam(value = "machineVersionUrl", required = false) machineVersionUrl: String?,
            @ApiParam(value = "machineVersionCode") @RequestParam(value = "machineVersionCode", required = false) machineVersionCode: Integer?,

            @ApiParam(value = "uiVersionName") @RequestParam(value = "uiVersionName", required = false) uiVersionName: String?,
            @ApiParam(value = "uiVersionUrl") @RequestParam(value = "uiVersionUrl", required = false) uiVersionUrl: String?,
            @ApiParam(value = "uiVersionCode") @RequestParam(value = "uiVersionCode", required = false) uiVersionCode: Integer?,

            @ApiParam(value = "otherFilesVersionName") @RequestParam(value = "otherFilesVersionName", required = false) otherFilesVersionName: String?,
            @ApiParam(value = "otherFilesVersionUrl") @RequestParam(value = "otherFilesVersionUrl", required = false) otherFilesVersionUrl: String?,
            @ApiParam(value = "otherFilesVersionCode") @RequestParam(value = "otherFilesVersionCode", required = false) otherFilesVersionCode: Integer?,


            @ApiParam(value = "selfVersionName") @RequestParam(value = "selfVersionName", required = false) selfVersionName: String?,
            @ApiParam(value = "selfVersionCode") @RequestParam(value = "selfVersionCode", required = false) selfVersionCode: Integer?,
            @ApiParam(value = "selfVersionUrl") @RequestParam(value = "selfVersionUrl", required = false) selfVersionUrl: String?

    ): Any {
//        Assert.notNullWithTips(httpServiceVersionCode, "httpServiceVersionCode")
//        Assert.notNullWithTips(httpServiceVersionName, "httpServiceVersionName")
//        Assert.notNullWithTips(httpServiceVersionUrl, "httpServiceVersionUrl")
//
//        Assert.notNullWithTips(machineVersionName, "machineVersionName")
//        Assert.notNullWithTips(machineVersionCode, "machineVersionCode")
//        Assert.notNullWithTips(machineVersionUrl, "machineVersionUrl")
//
//        Assert.notNullWithTips(uiVersionName, "uiVersionName")
//        Assert.notNullWithTips(uiVersionCode, "uiVersionCode")
//        Assert.notNullWithTips(uiVersionUrl, "uiVersionUrl")
//
//        Assert.notNullWithTips(otherFilesVersionName, "otherFilesVersionName")
//        Assert.notNullWithTips(otherFilesVersionCode, "otherFilesVersionCode")
//        Assert.notNullWithTips(otherFilesVersionUrl, "otherFilesVersionUrl")

        val newestVersionConfig: UsNewestVersionConfig = Request2ModelUtil.covert(UsNewestVersionConfig::class.java, request)

        val newestVersionConfigPage: Page<UsNewestVersionConfig> = usNewestVersionConfigService!!.queryAll(InstanceUtil.newHashMap("machineId", machineId))

        if (newestVersionConfigPage.total != 0) {
            var nvc = newestVersionConfigPage.records[0]
            newestVersionConfig.id = nvc.id
            newestVersionConfig.createTime = nvc.createTime
        } else {
            newestVersionConfig.machineId = machineId
        }


        val result = usNewestVersionConfigService.insertOrUpdateWithDefaultValue(newestVersionConfig)
        return setSuccessModelMap(modelMap, result)
    }
}